package com.jl.card.parse.part;

import opencard.opt.util.TLV;

import com.jl.card.JL;
import com.jl.card.parse.IAPDUComponent;
import com.jl.card.parse.TerminalProfileStructure;
import com.jl.card.parse.part.simpleTLV.SimpleTLVTool;
import com.jl.card.util.SimpleTLV;
import com.jl.util.HexUtil;

public class CDATAPart extends APDUPart{

	public CDATAPart(IAPDUComponent parent, byte[] part) {
		super(parent, part);
		analyse();
	}
	
	@Override
	public String getSummary() {
		return "CDATA:" + HexUtil.toHexCompact(part);
	}
	
	@Override
	public void analyse() {
		if(parent instanceof APDUComponentManager){
			byte ins = ((APDUComponentManager)parent).getIns();
			switch (ins) {
			case JL.INS_TERMINAL_RESPONSE:
				SimpleTLVTool.analyse(this,new SimpleTLV(null, part));
				break;
			case JL.INS_ENVELOPE:
				new TLVPart(this, part);
				TLV tlv = new TLV(part);
				SimpleTLV stlv2 = new SimpleTLV(null, tlv.valueAsByteArray());
				SimpleTLVTool.analyse(this,stlv2);
				break;
			case JL.INS_TERMINAL_PROFILE:
				TerminalProfileStructure tfp = new TerminalProfileStructure(part);
				for(int i = 0 ; i < part.length; i++){
					NormalPart p = new NormalPart(this, new byte[]{part[i]}, "---" + (i + 1) + "---");
					for(int j = 1 ; j <= 8; j++){
						String descript = (tfp.isSupportedByTerminal(i + 1, j) ? "1 " : "0 ") + tfp.getName(i + 1, j);
						new NormalPart(p, null, descript);
					}
				}
				break;
			default:
				break;
			}
		}
	}
	
}
